package com.spzx.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.common.rabbit.service.RabbitService;
import com.spzx.order.api.OrderInfoFeignClient;
import com.spzx.order.api.domain.OrderInfo;
import com.spzx.order.api.domain.OrderItem;
import com.spzx.payment.domain.PaymentInfo;
import com.spzx.payment.mapper.PaymentInfoMapper;
import com.spzx.payment.service.PaymentInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * @ClassName PaymentInfoServiceImpl
 * @Author 波
 * @Date 2024/8/16 15:52
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {
    @Resource
    private OrderInfoFeignClient orderInfoFeignClient;
    @Resource
    private RabbitService rabbitService;

    /**
     * 保存支付订单信息
     * @param orderNo 订单号
     * @return PaymentInfo
     */
    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        PaymentInfo paymentInfo = baseMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo));
        if(paymentInfo==null){
            R<OrderInfo> orderInfoResult = orderInfoFeignClient.getByOrderNo(orderNo, SecurityConstants.INNER);
            if(orderInfoResult.getCode()==R.FAIL){
                throw new ServiceException(orderInfoResult.getMsg());
            }
            OrderInfo orderInfo = orderInfoResult.getData();
            paymentInfo=new PaymentInfo();
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setOrderNo(orderInfo.getOrderNo());
            paymentInfo.setPayType(2L);
            paymentInfo.setAmount(orderInfo.getTotalAmount());
            paymentInfo.setPaymentStatus("0");

            String content="";
            for (OrderItem orderItem : orderInfo.getOrderItemList()) {
                content+=orderItem.getSkuName()+" ";
            }
            paymentInfo.setContent(content);
            baseMapper.insert(paymentInfo);
        }
        return paymentInfo;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePaymentInfoStatus(Map<String, String> paramMap) {
        PaymentInfo paymentInfo = baseMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, paramMap.get("out_trade_no")));
        if(paymentInfo.getPaymentStatus().equals("1")){
            return;
        }
        paymentInfo.setTradeNo(paramMap.get("trade_no"));
        paymentInfo.setPaymentStatus("1");
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(paramMap));
        baseMapper.updateById(paymentInfo);

        //基于MQ通知订单系统，修改订单状态
        rabbitService.sendMessage(MqConst.EXCHANGE_PAYMENT_PAY, MqConst.ROUTING_PAYMENT_PAY, paymentInfo.getOrderNo());
    }
}
